﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using System;
using System.ComponentModel;

namespace Property.Workers
{
    public class CatchFunctionWorkerDecorator<T> : FunctionWorkerDecorator<T>
    {
        protected CatchFunctionWorkerDecorator(IFunctionWorkerPrototype<T> compositionBasePrototype, Func<T> getValue, Action<Exception> handler)
            : base(compositionBasePrototype, getValue)
        {
            if (handler == null) throw new ArgumentNullException("handler");

            _Handler = handler;
        }

        //=//

        private readonly Action<Exception> _Handler;

        protected override void Completed(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                _Handler(e.Error);
            else
                OnRunWorkerCompleted(e);
        }

        public override void RunWorkerAsync()
        {
            _CompositionBase.RunWorkerAsync();
        }
    }
}
